<%
'*******************************************************************************
' Copyright (C) 2006 Google Inc.
' 
' Licensed under the Apache License, Version 2.0 (the "License");
' you may not use this file except in compliance with the License.
' You may obtain a copy of the License at
'  
'      http://www.apache.org/licenses/LICENSE-2.0
'  
' Unless required by applicable law or agreed to in writing, software
' distributed under the License is distributed on an "AS IS" BASIS,
' WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
' See the License for the specific language governing permissions and
' limitations under the License.
'*******************************************************************************

'*******************************************************************************
' Please refer to the Google Checkout ASP Sample Code Documentation
' for requirements and guidelines on how to use the sample code.
'
' This sample code demonstrates how to utilize the Google Checkout ASP Client 
' Library to generate <checkout-shopping-cart> XML (referred below as 
' "Cart XML") and to place a new order by posting the cart XML via 
' server-to-server POST.
' 
' This alternative method of posting Checkout API requests contains
' the following steps:
'
'   1. The Merchant web page displays a Google Checkout button. The button
'          is contained in a form that submits to the merchant's server.
'   2. When the customer clicks the Google Checkout button, the merchant's
'          server generates the <checkout-shopping-cart> XML and posts the
'          shopping cart directly to the Google Checkout server.
'   3. The Google Checkout server responds to the merchant's request with 
'          a <checkout-redirect> XML response, which contains a <redirect-url>.
'   4. The Merchant returns an HTTP 302 redirect to the customer's browser.
'   5. The customer's browser follows the redirect, bringing the customer
'          to the Google Checkout order page.
'
'*******************************************************************************

' Include PHP libraries used to create shopping cart and 
' send server-to-server request.
%>

<!--#INCLUDE file="GlobalAPIFunctions.asp"--> 
<!--#INCLUDE file="CheckoutAPIFunctions.asp"--> 
<!--#INCLUDE file="ResponseHandlerAPIFunctions.asp"--> 

<% 

' This page posts requests to itself. When you navigate to this page,
' it will display an HTML page containing information about server-to-server
' Checkout API requests. It will also display a Google Checkout button.
' The HTML for this page is contained within the following "if" block.
' When you click the Google Checkout button, the page will submit an
' HTTP POST request to itself. This request will set the
' "REQUEST_METHOD" server variable equal to "POST". As such, the HTML
' form will not display and the code within the "elseif" portion of this
' "if-elseif" block will execute. That code executes a server-to-server
' HTTP POST request and then redirects the user to the <redirect-url>
' returned in the Google Checkout <checkout-redirect> response.
'
' +++ CHANGE ME +++
' If your site submits server-to-server Checkout API requests, you may want
' to change the form's action so that the form submits to a different page.

If Request.ServerVariables("REQUEST_METHOD") <> "POST" Then
%>
<html>
<head>
    <style type="text/css">@import url(googleCheckout.css);</style>
</head>
<body>
    <p style="text-align:center">
    <table class="table-1" cellspacing="5" cellpadding="5">
        <tr><td style="padding-bottom:20px"><h2>
        Place a New Order using the Alternative Method
        </h2></td></tr>

        <tr><td>
            <p><b>The alternative method of posting Checkout API requests 
                  contains the following steps:</b></p>
            <p><ol>
              <li>The Merchant web page displays a Google Checkout button. 
                  The button is contained in a form that submits to the 
                  merchant's server.</li>
              <li>When the customer clicks the Google Checkout button, the 
                  merchant's server generates the 
                  &lt;checkout-shopping-cart&gt; XML and posts the shopping 
                  cart directly to the Google Checkout server.</li>
              <li>The Google Checkout server responds to the merchant's 
                  request with a &lt;checkout-redirect&gt; XML response, which 
                  contains a &lt;redirect-url&gt;.</li>
              <li>The Merchant returns an HTTP 302 redirect to the 
                  customer's browser.</li>
              <li>The customer's browser follows the redirect, bringing 
                  the customer to the Google Checkout order page.</li>
            </ol></p>
            <p>&nbsp;</p>

            <p><b>Click on the Checkout button to post a cart using this 
            Alternative Method.</b></p>

            <%
                ' Google Checkout button implementation

                Dim buttonW
                Dim buttonH
                Dim buttonStyle
                Dim buttonVariant
                Dim buttonLoc
                Dim buttonSrc
                
                buttonW = "180"
                buttonH = "46"
                buttonStyle = "white"
                buttonVariant = "text"
                buttonLoc = "en_US"
                buttonSrc = _
                    "https://sandbox.google.com/buttons/checkout.gif" & _
                    "?merchant_id=" & strMerchantId & _
                    "&w=" & buttonW & _
                    "&h=" & buttonH & _
                    "&style=" & buttonStyle & _
                    "&variant=" & buttonVariant & _
                    "&loc=" & buttonLoc
            %>

            <p><form method="POST" 
                action="<%=Request.ServerVariables("REQUEST_URI")%>">
                <input type="image" name="Checkout" alt="Checkout" 
                src="<%=buttonSrc%>" height="<%=buttonH%>" width="<%=buttonW%>">
                </form></p>

        </td></tr>
    </table>
    </p>
</body>
</html>
<%
ElseIf Request.ServerVariables("REQUEST_METHOD") = "POST" Then

'**********************Create checkout shopping cart **************************'

' Define objects used to create the shopping cart
Dim domTaxArea
Dim domShippingRestrictions

Dim elemItemName
Dim elemItemDescription
Dim elemQuantity
Dim elemUnitPrice
Dim elemMerchantItemId
Dim elemTaxTableSelector
Dim elemShippingTaxed
Dim elemRate
Dim elemTaxAreaState
Dim elemTaxAreaCountry
Dim elemTaxAreaZip
Dim elemPrice
Dim elemMerchantCalculationsUrl
Dim elemAcceptMerchantCoupons
Dim elemAcceptGiftCertificates
Dim elemEditCartUrl
Dim elemContinueShoppingUrl
Dim elemRequestBuyerPhoneNumber
Dim elemMerchantPrivateItemData
Dim elemMerchantPrivateData

Dim attrName
Dim attrMerchantCalculated
Dim attrStandalone
Dim attrAllowedCountryArea
Dim attrExcludedCountryArea

Dim dtmCartExpiration

Dim strAllowedState
Dim strAllowedZip
Dim strExcludedState
Dim strExcludedZip

Dim arrayAllowedState
Dim arrayAllowedZip
Dim arrayExcludedState
Dim arrayExcludedZip

Dim checkoutPostData
Dim diagnoseResponse

' Build XML for items in the shopping cart. The shopping cart
' has the following structure:
' <shopping-cart>
'     <items>
'         <item>
'             <item-name>Dry Food Pack AA1453</item-name>
'             <item-description>This food is very nutritious.</item-description>
'             <quantity>1</quantity>
'             <unit-price currency="USD">35.00</unit-price>
'             <tax-table-selector>food</tax-table-selector>
'             <merchant-private-item-data>
'               <item-note>Product Number N15037124531</item-note>
'             </merchant-private-item-data>
'         </item>
'         <!-- More items may be included using the same XML structure -->
'     </items>
' </shopping-cart>

' The XML for an individual item is created by defining data fields
' for the item and then calling the createItem() function, which
' is in the GlobalAPIFunctions.asp file.

'  * +++ CHANGE ME +++
' You will need to modify calls to functions like createItem,
' addAllowedAreas, addExcludedAreas, createFlatRateShipping and
' numerous others in this file to reflect the items in the
' customer's shopping cart, the shipping options available for
' those items and the tax tables that you use to calculate taxes.

' Specify item data and create an item to include in the order
elemItemName = "Dry Food Pack AA1453" 
elemItemDescription = "A pack of highly nutritious dried food for emergency " _
    & "- store in your garage for up to one year!!"
elemQuantity = "1"
elemUnitPrice = "35.00"
elemMerchantItemId = "GGLAA1453"
elemTaxTableSelector = "food"
createItem elemItemName, elemItemDescription, elemQuantity, elemUnitPrice, _
    elemMerchantItemId, elemTaxTableSelector, elemMerchantPrivateItemData

' Specify item data and create a second item to include in the order
elemItemName = "MegaSound 2GB MP3 Player"
elemItemDescription = "Portable MP3 player - stores 500 songs"
elemQuantity = "1"
elemUnitPrice = "178.00"
elemMerchantItemId = "MGS2GBMP3"
elemTaxTableSelector = ""
elemMerchantPrivateItemData = _
    "<item-note>Product Number N15037124531</item-note>"
createItem elemItemName, elemItemDescription, elemQuantity, elemUnitPrice, _
    elemMerchantItemId, elemTaxTableSelector, elemMerchantPrivateItemData

' Specify an expiration date for the order and build <shopping-cart>
dtmCartExpiration = "2006-12-31T23:59:59"
elemMerchantPrivateData = _
    "<merchant-note>My order number 9876543</merchant-note>"
createShoppingCart dtmCartExpiration, elemMerchantPrivateData

' Create list of areas where a particular shipping option is available
attrAllowedCountryArea = "ALL"  ' OR: "CONTINENTAL_48", "FULL_50_STATES"
arrayAllowedState = Array()     ' Ex: Array("CA", "NY", "DC", "NC")
arrayAllowedZip = Array()       ' Ex: Array("94043", "94086", "91801", "91362")
Set domShippingRestrictions = addAllowedAreas(attrAllowedCountryArea, _
    arrayAllowedState, arrayAllowedZip)

' Create list of areas where a particular shipping option is not available
attrExcludedCountryArea = ""
arrayExcludedState = Array("AL", "MA", "MT", "WA")
arrayExcludedZip = Array()
Set domShippingRestrictions = addExcludedAreas(attrExcludedCountryArea, _
    arrayExcludedState, arrayExcludedZip)

' Create a <flat-rate-shipping> option with shipping restrictions
attrName = "UPS Ground"
elemPrice = "8.50"
createFlatRateShipping attrName, elemPrice, domShippingRestrictions

' The call to the createMerchantCalculatedShipping function is commented out 
' because a shopping cart can not contain a <merchant-calculated-shipping> 
' option as well as other types of shipping options. 
' To use  <merchant-calculated-shipping> options with this demo, you would 
' need to  uncomment the next four lines of code and also comment out the 
' calls to the createFlatRateShipping and createPickup functions.
'
' attrName = "SuperShip"
' elemPrice = "10.00"
' domShippingRestrictions = ""
' createMerchantCalculatedShipping attrName, elemPrice, domShippingRestrictions

' Create a <pickup> shipping option
attrName = "Pickup"
elemPrice = "0.00"
createPickup attrName, elemPrice

' Create tax tables for the order. Tax tables have the
' following XML structure:
' <tax-tables>
'     <default-tax-table>
'         <tax-rules>
'             <default-tax-rule>
'                 <shipping-taxed>true</shipping-taxed>
'                 <rate>0.0825</rate>
'                 <tax-area>
'                     <!-- could also contain country or zip areas>
'                     <us-state-area>
'                         <state>NY</state>
'                     </us-state-area>
'                 </tax-area>
'             </default-tax-rule>
'         </tax-rules>
'     </default-tax-table>
'     <alternate-tax-tables>
'         <alternate-tax-table>
'             <alternate-tax-rules>
'                 <alternate-tax-rule>
'                     <rate>0.0825</rate>
'                     <tax-area>
'                         <!-- could also contain country or zip areas>
'                         <us-state-area>
'                             <state>NY</state>
'                         </us-state-area>
'                     </tax-area>
'                 </alternate-tax-rule>
'             </alternate-tax-rules>
'         </alternate-tax-table>
'     </alternate-tax-tables>
' </tax-tables>
'
' +++ CHANGE ME +++
' You will need to update the tax tables to match those
' used to calculate taxes for your store

' Build <default-tax-table>
elemRate = "0.0825"
elemTaxAreaCountry = "ALL"
Set domTaxArea = createTaxArea("country", elemTaxAreaCountry)
elemShippingTaxed = "false"
createDefaultTaxRule elemRate, domTaxArea, elemShippingTaxed

elemRate = "0.0800"
elemTaxAreaState = "NY"
Set domTaxArea = createTaxArea("state", elemTaxAreaState)
elemShippingTaxed = "true"
createDefaultTaxRule elemRate, domTaxArea, elemShippingTaxed

' Build an <alternate-tax-table>
elemRate = "0.0225"
elemTaxAreaState = "CA"
Set domTaxArea = createTaxArea("state", elemTaxAreaState)
createAlternateTaxRule elemRate, domTaxArea

elemRate = "0.0200"
elemTaxAreaState = "NY"
Set domTaxArea = createTaxArea("state", elemTaxAreaState)
createAlternateTaxRule elemRate, domTaxArea

attrStandalone = "false"
attrName = "food"
createAlternateTaxTable attrStandalone, attrName


' Build another <alternate-tax-table>
elemRate = "0.0500"
elemTaxAreaCountry = "FULL_50_STATES"
Set domTaxArea = createTaxArea("country", elemTaxAreaCountry)
createAlternateTaxRule elemRate, domTaxArea

elemRate = "0.0600"
elemTaxAreaZip = "9404*"
Set domTaxArea = createTaxArea("zip", elemTaxAreaZip)
createAlternateTaxRule elemRate, domTaxArea

attrStandalone = "true"
attrName = "drug"
createAlternateTaxTable attrStandalone, attrName


' Build <tax-tables>
attrMerchantCalculated = "true"
createTaxTables attrMerchantCalculated


' Specify A URL to which Google Checkout should send Merchant Calculations API
' (<merchant-calculation-callback>) requests and create the
' <merchant-calculations> XML for a Checkout API request.
'
' +++ CHANGE ME +++
' If you are implementing the Merchant Calculations API, you need to
' uncomment the following lines of code, which create the
' <merchant-calculations> XML in a Checkout API response. You also
' need to update the value of the elemMerchantCalculationsUrl variable
' to the URL to which Google Checkout should send 
' <merchant-calculation-callback> requests.
elemMerchantCalculationsUrl = _
    "http://www.example.com/shopping/MerchantCalculationCallback.asp"
elemAcceptMerchantCoupons = "true"
elemAcceptGiftCertificates = "true"
createMerchantCalculations elemMerchantCalculationsUrl, _
    elemAcceptMerchantCoupons, elemAcceptGiftCertificates

' +++ CHANGE ME +++
' The elemEditCartUrl variable identifies a URL that the customer can
' link to to edit the contents of the shopping cart.
' The elemContinueShoppingUrl variable identifies a URL that the
' customer can link to to continue shopping.
' If you are providing of these options to your customers, you need
' to insert the appropriate URLs for these variables.
' e.g. elemEditCartUrl = "http://www.example.com/shopping/edit";
'      elemContinueShoppingUrl = "http://www.example.com/shop/continue";
elemEditCartUrl = "http://www.example.com/shopping/edit"
elemContinueShoppingUrl = "http://www.example.com/shopping/continue"
elemRequestBuyerPhoneNumber = "true"

' Build the <merchant-checkout-flow-support> element in the Checkout
' API request.
createMerchantCheckoutFlowSupport elemEditCartUrl, _
    elemContinueShoppingUrl, elemRequestBuyerPhoneNumber

' Get <checkout-shopping-cart> XML
Dim xmlCart
xmlCart = createCheckoutShoppingCart

' Post the cart XML via a server-to-server POST and 
' capture the <checkout-redirect> response.
Dim transmitResponse
transmitResponse = SendRequest(xmlCart, requestUrl)

' Process the response by redirecting the user to the checkout redirect URL
ProcessXmlData(transmitResponse)


' Free object
Set cryptObj = Nothing
Set domTaxArea = Nothing
Set domShippingRestrictions = Nothing

End If
%>
